package my.cn.bdqn.life;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import my.cn.bdqn.life.data.DataManager;
import my.cn.bdqn.life.net.URLParam;
import my.cn.bdqn.life.net.URLProtocol;
import my.cn.bdqn.life.net.impl.URLProtocolImpl;

import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DelicaciesActivity extends Activity {
	TextView header;
	private List<DataManager.Food> data=new ArrayList();
	private ListView listView;
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case 0:
				adapter.notifyDataSetChanged();
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_eat);
		header=(TextView)findViewById(R.id.headtext);
		header.setText(R.string.h_eat);
		listView=(ListView)findViewById(R.id.eat_list);
		listView.setAdapter(adapter);
		new Thread(){
			@Override
			public void run(){
				getData();
				handler.sendEmptyMessage(0);
			}
		}.start();	
		listView.setOnItemClickListener(new ListView.OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				DataManager.Food f=data.get(pos);
				Intent intent=new Intent(DelicaciesActivity.this,DelicaciesDetail.class);
				intent.putExtra("data",f);
				startActivity(intent);
			}
			
		});
		
	}
	private void getData(){
		Map<String, String> map = new HashMap();
		map.put("cmd","301");		
		HttpPost post = URLParam.createURL(map);
		URLProtocol urlP = new URLProtocolImpl();
	    JSONObject json = urlP.getData(map);
		int code=1;
		try{
		 code=json.getInt("code");
		 if(code==0){
			 JSONArray array=json.getJSONArray("list");
			 for(int i=0;i<array.length();i++){
				 JSONObject o=array.getJSONObject(i);
				 DataManager.Food food=new DataManager.Food(o.getInt("did")
						 , o.getInt("image")
						 , o.getInt("avg")
						 , o.getString("name")
						 , o.getString("label")
						 , o.getString("addr"));
				 int image=food.getImage();
					File file=new File("/mnt/sdcard/freelife/"+image);
					if(!file.exists()){
						URL url=new URL("http://118.186.211.92:8080/LeisureLifeServer/dealcmd?cmd=4&image="+image);
						InputStream is=url.openStream();
						if(!file.getParentFile().exists()){
							file.getParentFile().mkdirs();
						}
						FileOutputStream out=new FileOutputStream(file);
						byte[]buf=new byte[1024];
						int len;
						while((len=is.read(buf))!=-1){
							out.write(buf,0,len);
						}
						out.close();
						is.close();					
					}
				 data.add(food);
			 }
		 }	
		}catch(Exception e){}
				
	}//getData
	private BaseAdapter adapter=new BaseAdapter(){

		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		public View getView(int pos, View view, ViewGroup arg2) {
			// TODO Auto-generated method stub
			DataManager.Food f=data.get(pos);
			view=getLayoutInflater().inflate(R.layout.eat_content,null);
			ImageView photo=(ImageView)view.findViewById(R.id.restaurant_photo);
			TextView name=(TextView)view.findViewById(R.id.restaurant);
			TextView label=(TextView)view.findViewById(R.id.eat_label);
			TextView addr=(TextView)view.findViewById(R.id.eat_address);
			TextView avg=(TextView)view.findViewById(R.id.consume);
			Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/freelife/"+f.getImage());
			photo.setImageBitmap(bm);
			name.setText(f.getName());
			label.setText(f.getLabel());
			addr.setText(f.getAddr());
			avg.setText(Integer.toString(f.getAvg()));
			return view;
		}
		
	};

	
	
	
	
	
	
	
}

